//
//  WmcBaseBrandView.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/6/13.
//

import UIKit
import YPBedrock

class WmcBaseBrandView: WmcBaseView {
    lazy var contentView: UIView = {
        let view = UIView()
        return view
    }()

    lazy var imageView: UIImageView = {
        let imageView = UIImageView()
        return imageView
    }()

    /// 品牌图字段配置
    var optionBrand: WmcWatermarkOptionModel?

    /// 品牌图配置
    var brand: WmcWatermarkOptionModel.Brand?

    /// 最大大小
    var maxSize: CGSize = 150.yp.cgSize

    /// 是否需要自动调整自己大小
//    var isAutoLayoutSelfSize: Bool = true

    /// 点击品牌图时候，需要触发的Block
    var brandHitBrandCallBlock: YPBlock.Return<UIView?>?

    /// 品牌图确定大小后的回调
    var brandFitSizeBlock: YPBlock.OneParam<CGSize>?

    /// 创建品牌图视图
    class func CreateBrandTemplateView(_ optionBrand: WmcWatermarkOptionModel) -> WmcBaseBrandView? {
        let brand = optionBrand.brand
        var brandView: WmcBaseBrandView?
        switch brand.templateId {
        case 0: brandView = WmcEmptyBrandView()
        default: break
        }

        guard let brandView = brandView else {
            WmcLogManager.Log("品牌图类型设置失败！请检查模板是否符合！")
            return nil
        }
        return brandView
    }

    func loadImage(_ wmcImage: WmcImage) {
        // 占位图
        drawImage("wm_brand_placeholder".wmc_image)
        // 再加载
        wmcImage.loadUIImage { [weak self] image in
            guard let weakSelf = self else { return }
            weakSelf.drawImage(image)
        }
    }

    func drawImage(_ image: UIImage) {
        imageView.image = image
        brandFitSizeBlock?(frame.size)
    }

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        if let block = brandHitBrandCallBlock {
            return block()
        }
        let supView = super.hitTest(point, with: event)
        return supView
    }

    override func addSubview(_ view: UIView) {
        if view == contentView {
            super.addSubview(view)
        } else {
            contentView.addSubview(view)
        }
    }

    override func layoutSubviews() {
        super.layoutSubviews()

//        contentView.layer.anchorPoint = .init(x: 0, y: 1)
//        contentView.layer.position = CGPoint(x: 0, y: anchorPositionView.frame.height)
    }

    override func makeUI() {
        backgroundColor = nil
        isUserInteractionEnabled = true

        addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }

    func configOptionBrand(_ optionBrand: WmcWatermarkOptionModel) {
        self.optionBrand = optionBrand
        self.brand = optionBrand.brand
        // 加载品牌图，先判断是否是本地图
        if let localWmcImage = optionBrand.brand.localImage {
            loadImage(localWmcImage)
        } else {
            if optionBrand.content.isEmpty {
                loadImage(.init(name: "wm_brand_placeholder"))
            } else {
                loadImage(.init(url: optionBrand.content))
            }
        }
    }

    func configBrand() {
        guard let brand = brand else { return }
        let scale = (CGFloat(brand.size) / 100) + 0.8
        configBrandScale(scale)
        configBrandAlpha(CGFloat(brand.alpha))
        configBrandCornerRadius(brand.cornerRadius)
    }

    func configBrandScale(_ scale: CGFloat) {}

    func configBrandAlpha(_ alpha: CGFloat) {
        let alpha = 1 - (alpha / 100)
        contentView.alpha = alpha
    }

    func configBrandCornerRadius(_ cornerRadius: CGFloat) {
        contentView.layer.cornerRadius = cornerRadius
        contentView.layer.masksToBounds = true
    }

    /// 大小计算完成
    func sizeCalculateComplete() {}
}
